package com.draby.starfantasymaze.activities.game;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.dto.map.Block;
import com.draby.starfantasymaze.dto.map.Map;
import com.draby.starfantasymaze.enums.Customize;
import com.draby.starfantasymaze.enums.Direction;
import com.draby.starfantasymaze.enums.Mode;
import com.draby.starfantasymaze.enums.StatusCode;
import com.draby.starfantasymaze.methods.game.CreateGame;
import com.draby.starfantasymaze.methods.game.GameTimer;
import com.draby.starfantasymaze.utils.FileUtil;
import com.draby.starfantasymaze.utils.PixelConversionUtil;
import com.draby.starfantasymaze.utils.SystemUiUtil;
import com.draby.starfantasymaze.utils.ToastUtil;

public class GameSceneActivity extends AppCompatActivity {

    // 困难模式新玩法数据
    private boolean isFind = false;
    // 无尽模式的数据
    private int recordInfinite;
    private int infiniteDistance;
    private int distance = 0;
    // 文件存放父目录与文件名
    private String parent;
    private String data_fileName;
    private String achievement_fileName;
    // 游戏计时器
    private GameTimer gameTimer;
    /*
    * 保存玩家和终点的坐标
    * record[0] 记录玩家当前坐标
    * record[1] 记录终点坐标
    * record[2] 记录npc坐标
    * record[n][0] 记录行坐标索引
    * record[n][1] 记录列坐标索引
    * */
    private final int[][] record = new int[3][2];
    // 需要用到的图片（玩家、终点和NPC图片）
    private Bitmap player;
    private Bitmap destination;
    private Bitmap npc;
    // 游戏画笔
    private Paint linePaint;
    private Paint rectPaint;
    // 游戏格子的宽高（单位：dp）
    private final int blockHeight = 40;
    private final int blockWidth = 40;
    // 游戏视图
    private View gameView;
    // 游戏地图与模式
    private Map map;
    private Mode mode;
    private Customize customize;
    // 控件
    private ImageButton ib_pause;
    private TextView tv_timer;
    private ImageButton ib_up;
    private ImageButton ib_down;
    private ImageButton ib_left;
    private ImageButton ib_right;
    private LinearLayout ll_travel;
    private TextView tv_travel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏 状态栏 与 导航栏
        SystemUiUtil.hideSystemBar(getWindow());
        setContentView(R.layout.activity_game_scene);

        // 设置文件存放父目录与文件名
        parent = getString(R.string.file_directory);
        data_fileName = getString(R.string.fileName_data);
        achievement_fileName = getString(R.string.fileName_achievement);

        // 找到控件
        ib_pause = findViewById(R.id.ib_pause);
        tv_timer = findViewById(R.id.tv_timer);
        ib_up = findViewById(R.id.ib_up);
        ib_down = findViewById(R.id.ib_down);
        ib_left = findViewById(R.id.ib_left);
        ib_right = findViewById(R.id.ib_right);
        ll_travel = findViewById(R.id.ll_travel);
        tv_travel = findViewById(R.id.tv_travel);

        // 初始化游戏数据
        initData();
        // 初始化游戏地图的视图
        initGameView();
        // 设置按钮的点击事件监听器
        setOnClickListener();
        // 根据游戏模式设置顶部视图
        setModeView();
    }

    /**
     * 设置按钮点击事件监听器
     */
    private void setOnClickListener() {
        OnClick onClick = new OnClick();
        ib_pause.setOnClickListener(onClick);
        ib_up.setOnClickListener(onClick);
        ib_right.setOnClickListener(onClick);
        ib_down.setOnClickListener(onClick);
        ib_left.setOnClickListener(onClick);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        // 获取传输过来的数据，初始化地图与模式
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            map = (Map) bundle.getSerializable("Map");
            mode = (Mode) bundle.getSerializable("Mode");
            customize = (Customize) bundle.getSerializable("Customize");
        } else {
            ToastUtil.showMsg(this, "获取 bundle 失败，请重新尝试！");
            // 结束游戏 activity
            finish();
        }

        // 初始化玩家与终点的坐标数据
        Block[][] maze = map.getMaze();
        // 遍历迷宫中的所有格子，找到玩家和终点的位置
        for (int i = 0; i < map.getHeight(); i++) {
            for (int j = 0; j < map.getWidth(); j++) {
                Block block = maze[i][j];
                StatusCode statusCode = block.getStatusCode();
                switch (statusCode) {
                    case DESTINATION:
                        // 记录终点坐标
                        record[1][0] = block.getRowIdx();
                        record[1][1] = block.getColIdx();
                        break;
                    case PLAYER:
                        // 记录玩家坐标
                        record[0][0] = block.getRowIdx();
                        record[0][1] = block.getColIdx();
                        break;
                    case BABY_DRAGON:
                        // 记录npc坐标
                        record[2][0] = block.getRowIdx();
                        record[2][1] = block.getColIdx();
                        break;
                }
            }
        }

        // 初始化玩家、终点和NPC图片
        player = BitmapFactory.decodeResource(getResources(), R.drawable.player);
        destination = BitmapFactory.decodeResource(getResources(), R.drawable.destination);
        // 困难模式
        if (Mode.HARD.equals(mode)) {
            player = BitmapFactory.decodeResource(getResources(), R.drawable.player_you);
            npc = BitmapFactory.decodeResource(getResources(), R.drawable.player);
        }
        // 生日模式
        if (Customize.BIRTHDAY.equals(customize)) {
            player = BitmapFactory.decodeResource(getResources(), R.drawable.player_you);
            destination = BitmapFactory.decodeResource(getResources(), R.drawable.destination_cake);
        }

        // 初始化游戏画笔
        linePaint = new Paint();
        linePaint.setColor(getColor(R.color.white));
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(PixelConversionUtil.dpToPx(this, 1));
        rectPaint = new Paint();

        // 初始化游戏计时器
        if (gameTimer != null) {
            gameTimer.pause();
        }
        gameTimer = new GameTimer(tv_timer);

        // 初始化无尽模式的数据
        recordInfinite = Integer.parseInt(FileUtil.getValue(parent, data_fileName, "recordInfinite"));
        infiniteDistance = Integer.parseInt(FileUtil.getValue(parent, data_fileName, "infiniteDistance"));
    }

    /**
     * 初始化游戏视图
     */
    private void initGameView() {
        // 找到游戏视图容器
        FrameLayout fl_game = findViewById(R.id.fl_game);
        // 实例化游戏视图
        gameView = new View(this) {
            // 重写绘制方法
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                // 获取迷宫
                Block[][] maze = map.getMaze();
                // 计算格子的像素宽高
                int blockPixelWidth = PixelConversionUtil.dpToPx(getContext(), blockWidth);
                int blockPixelHeight = PixelConversionUtil.dpToPx(getContext(), blockHeight);
                // 循环遍历迷宫中的所有格子
                for (int i = 0; i < map.getHeight(); i++) {
                    for (int j = 0; j < map.getWidth(); j++) {
                        // 取出当前格子
                        Block block = maze[i][j];
                        // 获取格子背景颜色并绘制
                        int backgroundColor = block.getBackgroundColor();
                        rectPaint.setColor(backgroundColor);
                        canvas.drawRect(j * blockPixelWidth,
                                i * blockPixelHeight,
                                (j + 1) * blockPixelWidth,
                                (i + 1) * blockPixelHeight, rectPaint);
                        // 获取状态码
                        StatusCode statusCode = block.getStatusCode();
                        // 格子的状态不为 UNREACHABLE 时则绘制格子的边
                        if (!StatusCode.UNREACHABLE.equals(statusCode)) {
                            // 不能向某个方向移动时，则绘制该格子对应方向的一条边
                            if (!block.getCanUp()) {
                                /*
                                 * 绘制上边
                                 * 开始点坐标为 (格子所在列索引*格子宽度, 格子所在行索引*格子高度)
                                 * 结束点坐标为 ((格子所在列索引+1)*格子宽度, 格子所在行索引*格子高度)
                                 * */
                                canvas.drawLine(j * blockPixelWidth,
                                        i * blockPixelHeight,
                                        (j + 1) * blockPixelWidth,
                                        i * blockPixelHeight, linePaint);
                            }
                            if (!block.getCanDown()) {
                                /*
                                 * 绘制下边
                                 * 开始点坐标为 (格子所在列索引*格子宽度, (格子所在行索引+1)*格子高度)
                                 * 结束点坐标为 ((格子所在列索引+1)*格子宽度, (格子所在行索引+1)*格子高度)
                                 * */
                                canvas.drawLine(j * blockPixelWidth,
                                        (i + 1) * blockPixelHeight,
                                        (j + 1) * blockPixelWidth,
                                        (i + 1) * blockPixelHeight, linePaint);
                            }
                            if (!block.getCanLeft()) {
                                /*
                                 * 绘制左边
                                 * 开始点坐标为 (格子所在列索引*格子宽度, 格子所在行索引*格子高度)
                                 * 结束点坐标为 (格子所在列索引*格子宽度, (格子所在行索引+1)*格子高度)
                                 * */
                                canvas.drawLine(j * blockPixelWidth,
                                        i * blockPixelHeight,
                                        j * blockPixelWidth,
                                        (i + 1) * blockPixelHeight, linePaint);
                            }
                            if (!block.getCanRight()) {
                                /*
                                 * 绘制右边
                                 * 开始点坐标为 ((格子所在列索引+1)*格子宽度, 格子所在行索引*格子高度)
                                 * 结束点坐标为 ((格子所在列索引+1)*格子宽度, (格子所在行索引+1)*格子高度)
                                 * */
                                canvas.drawLine((j + 1) * blockPixelWidth,
                                        i * blockPixelHeight,
                                        (j + 1) * blockPixelWidth,
                                        (i + 1) * blockPixelHeight, linePaint);
                            }
                        }
                        // 根据不同状态绘制格子中的内容
                        switch (statusCode) {
                            case UNREACHABLE:
                                // 不可到达的格子，背景颜色加深(暂时不用写)
                                break;
                            case DESTINATION:
                                // 终点所在格子，绘制终点图片
                                @SuppressLint("DrawAllocation") Rect destinationSource = new Rect(0, 0, destination.getWidth(), destination.getHeight());
                                @SuppressLint("DrawAllocation") Rect destinationTarget = new Rect(j * blockPixelWidth,
                                        i * blockPixelHeight,
                                        (j + 1) * blockPixelWidth,
                                        (i + 1) * blockPixelHeight);
                                canvas.drawBitmap(destination, destinationSource, destinationTarget, null);
                                break;
                            case PLAYER:
                                // 玩家所在格子，绘制玩家图片
                                @SuppressLint("DrawAllocation") Rect playerSource = new Rect(0, 0, player.getWidth(), player.getHeight());
                                @SuppressLint("DrawAllocation") Rect playerTarget = new Rect(j * blockPixelWidth,
                                        i * blockPixelHeight,
                                        (j + 1) * blockPixelWidth,
                                        (i + 1) * blockPixelHeight);
                                canvas.drawBitmap(player, playerSource, playerTarget, null);
                                break;
                            case BABY_DRAGON:
                                // npc所在格子，绘制npc图片
                                @SuppressLint("DrawAllocation") Rect npcSource = new Rect(0, 0, npc.getWidth(), npc.getHeight());
                                @SuppressLint("DrawAllocation") Rect npcTarget = new Rect(j * blockPixelWidth,
                                        i * blockPixelHeight,
                                        (j + 1) * blockPixelWidth,
                                        (i + 1) * blockPixelHeight);
                                canvas.drawBitmap(npc, npcSource, npcTarget, null);
                        }
                    }
                }
            }
        };
        // 获取地图的宽和高
        int width = map.getWidth();
        int height = map.getHeight();
        // 设置游戏区域大小
        gameView.setLayoutParams(new ViewGroup.LayoutParams(PixelConversionUtil.dpToPx(this, width * blockWidth), PixelConversionUtil.dpToPx(this, height * blockHeight)));
        // 设置视图的背景颜色
        gameView.setBackgroundColor(getColor(R.color.white_t50));
        // 将布局试图添加到游戏容器中
        fl_game.addView(gameView);
    }

    /**
     * 根据游戏模式设置顶部视图
     */
    private void setModeView() {
        switch (mode) {
            case EASY:
            case NORMAL:
            case HARD:
                ll_travel.setVisibility(View.GONE);
                gameTimer.start();
                break;
            case RANDOM:
                tv_timer.setVisibility(View.GONE);
                tv_travel.setText(String.valueOf(recordInfinite + 1));
                break;
            case CUSTOMIZE:
                switch (customize) {
                    case CUSTOMIZE:
                        break;
                    case BIRTHDAY:
                        ll_travel.setVisibility(View.GONE);
                        tv_timer.setText("Happy Birthday!");
                        break;
                }
                break;
        }
    }

    /**
     * 点击事件实现类，所有按钮点击事件在此实现
     */
    private class OnClick implements View.OnClickListener {
        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                // 暂停按钮
                case R.id.ib_pause:
                    // 暂停计时器
                    gameTimer.pause();
                    // 弹出提示框，可选择继续或返回主界面
                    AlertDialog.Builder builder = new AlertDialog.Builder(GameSceneActivity.this);
                    builder.setTitle("暂停中").setIcon(R.drawable.stop).setMessage("休息一下吧~💤").setCancelable(false);
                    builder.setPositiveButton("继续", (dialog, which) -> {
                        gameTimer.start();
                        dialog.dismiss();
                    }).setNeutralButton("返回", (dialog, which) -> {
                        // 结束游戏并返回主界面
                        finish();
                    });
                    builder.show();
                    break;
                // 向上移动按钮
                case R.id.ib_up:
                    move(Direction.UP);
                    gameView.invalidate();
                    break;
                // 向右移动按钮
                case R.id.ib_right:
                    move(Direction.RIGHT);
                    gameView.invalidate();
                    break;
                // 向下移动按钮
                case R.id.ib_down:
                    move(Direction.DOWN);
                    gameView.invalidate();
                    break;
                // 向左移动按钮
                case R.id.ib_left:
                    move(Direction.LEFT);
                    gameView.invalidate();
                    break;
            }
            // 判断是否到达终点
            checkArrive();
        }

        /**
         * 玩家移动的逻辑
         * @param direction 移动的方向
         */
        private void move(Direction direction) {
            // 获取玩家当前的坐标索引与所在格子
            int rolIdx = record[0][0];
            int colIdx = record[0][1];
            Block block = map.getMaze()[rolIdx][colIdx];
            // 判断移动的方向
            switch (direction) {
                // 向上移动
                case UP:
                    // 判断能否向上移动，不能则不执行任何动作
                    if (block.getCanUp()) {
                        // 将上方的格子状态码改为玩家，并更新记录玩家当前位置
                        map.getMaze()[rolIdx - 1][colIdx].setStatusCode(StatusCode.PLAYER);
                        record[0][0] = rolIdx - 1;
                        // 将之前所在的格子状态码改为已走过
                        block.setStatusCode(StatusCode.PLAYER_VISITED);
                        // 走过的距离加1
                        distance++;
                    }
                    break;
                // 以下逻辑类似向上移动
                case DOWN:
                    if (block.getCanDown()) {
                        map.getMaze()[rolIdx + 1][colIdx].setStatusCode(StatusCode.PLAYER);
                        record[0][0] = rolIdx + 1;
                        block.setStatusCode(StatusCode.PLAYER_VISITED);
                        distance++;
                    }
                    break;
                case LEFT:
                    if (block.getCanLeft()) {
                        map.getMaze()[rolIdx][colIdx - 1].setStatusCode(StatusCode.PLAYER);
                        record[0][1] = colIdx - 1;
                        block.setStatusCode(StatusCode.PLAYER_VISITED);
                        distance++;
                    }
                    break;
                case RIGHT:
                    if (block.getCanRight()) {
                        map.getMaze()[rolIdx][colIdx + 1].setStatusCode(StatusCode.PLAYER);
                        record[0][1] = colIdx + 1;
                        block.setStatusCode(StatusCode.PLAYER_VISITED);
                        distance++;
                    }
                    break;
            }
            // 困难模式才加此判断
            if (Mode.HARD.equals(mode) && !isFind) {
                checkFind();
            }
        }
    }

    /**
     * 检测玩家是否找到 npc
     */
    private void checkFind() {
        // 若记录的玩家当前坐标与npc坐标横纵坐标对应相同，则玩家找到npc
        if (record[0][0] == record[2][0] && record[0][1] == record[2][1]) {
            // 将玩家的图片替换为玩家与npc混合图片
            player = BitmapFactory.decodeResource(getResources(), R.drawable.player_mix);
            // 设置判断标志位 true
            isFind = true;
        }
    }

    /**
     * 检测玩家是否到达终点
     */
    private void checkArrive() {
        // 若记录的玩家当前坐标与终点坐标横纵坐标对应相同，则玩家到达终点
        if (record[0][0] == record[1][0] && record[0][1] == record[1][1]) {
            gameTimer.pause();
            int newRecord = gameTimer.getMinute() * 60 + gameTimer.getSecond() + (gameTimer.getMillisecond() < 50 ? 0 : 1);
            boolean isNew = false;
            switch (mode) {
                case EASY:
                    /*
                     * 简单模式，到达终点需要做如下事情：
                     * 0. 首次通过则完成成就，并判断限时成就
                     * 1. 获取简单模式的记录，并判断是否有记录，不为 0 则代表有记录
                     * 2. 比较这次时间与之前记录的时间，若这次时间更少则更新记录
                     * 3. 弹出对话框显示所用时间与信息
                     * 4. 用户确认后退出当前 activity
                     * 普通与困难模式类似简单模式
                     * */
                    if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "来散步吧")) {
                        FileUtil.finishAchievementByName(parent, achievement_fileName, "来散步吧");
                    }
                    if (newRecord <= 9) {
                        if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "小菜一碟")) {
                            FileUtil.finishAchievementByName(parent, achievement_fileName, "小菜一碟");
                        }
                    }
                    int recordEasy = Integer.parseInt(FileUtil.getValue(parent, data_fileName, "recordEasy"));
                    if (recordEasy == 0 || newRecord < recordEasy) {
                        isNew = true;
                    }
                    if (isNew) {
                        FileUtil.setValue(parent, data_fileName, "recordEasy", String.valueOf(newRecord));
                    }
                    notifyPlayer(newRecord, isNew);
                    break;
                case NORMAL:
                    if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "这里怎么走")) {
                        FileUtil.finishAchievementByName(parent, achievement_fileName, "这里怎么走");
                    }
                    if (newRecord <= 27) {
                        if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "轻轻松松")) {
                            FileUtil.finishAchievementByName(parent, achievement_fileName, "轻轻松松");
                        }
                    }
                    int recordNormal = Integer.parseInt(FileUtil.getValue(parent, data_fileName, "recordNormal"));
                    if (recordNormal == 0 || newRecord < recordNormal) {
                        isNew = true;
                    }
                    if (isNew) {
                        FileUtil.setValue(parent, data_fileName, "recordNormal", String.valueOf(newRecord));
                    }
                    notifyPlayer(newRecord, isNew);
                    break;
                case HARD:
                    if (isFind) {
                        if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "完全迷路了")) {
                            FileUtil.finishAchievementByName(parent, achievement_fileName, "完全迷路了");
                        }
                        if (newRecord <= 60) {
                            if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "带不动")) {
                                FileUtil.finishAchievementByName(parent, achievement_fileName, "带不动");
                            }
                        }
                    } else {
                        if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "回不了家了")) {
                            FileUtil.finishAchievementByName(parent, achievement_fileName, "回不了家了");
                        }
                        if (newRecord <= 52) {
                            if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "不带了")) {
                                FileUtil.finishAchievementByName(parent, achievement_fileName, "不带了");
                            }
                        }
                    }
                    int recordHard = Integer.parseInt(FileUtil.getValue(parent, data_fileName, "recordHard"));
                    if (recordHard == 0 || newRecord < recordHard) {
                        isNew = true;
                    }
                    if (isNew) {
                        FileUtil.setValue(parent, data_fileName, "recordHard", String.valueOf(newRecord));
                    }
                    notifyPlayer(newRecord, isNew);
                    break;
                case RANDOM:
                    /*
                    * 无尽模式，到达终点需要做如下事情：
                    * 1. 将无尽模式的层数加1
                    * 2. 将无尽模式行走的距离加 走过的格子数
                    * 3. 弹出对话框提示让玩家选择下一关或返回
                    *    a. 返回则结束当前的 activity
                    *    b. 下一关则开始新的游戏 activity，将数据传输过去，并结束当前的 activity
                    * */
                    FileUtil.setValue(parent, data_fileName, "recordInfinite", String.valueOf(recordInfinite + 1));
                    FileUtil.setValue(parent, data_fileName, "infiniteDistance", String.valueOf(infiniteDistance + distance));
                    AlertDialog.Builder builder = new AlertDialog.Builder(GameSceneActivity.this);
                    builder.setTitle("通过 " + (recordInfinite + 1) + " 区")
                            .setMessage("旅途 " + (recordInfinite + 1) + " 区，行走了：" + distance + " 米！").setCancelable(false)
                            .setPositiveButton("下一关", (dialog, which) -> {
                                CreateGame createGame = new CreateGame(Mode.RANDOM);
                                map = createGame.createGame();
                                if (map != null) {
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("Map", map);
                                    bundle.putSerializable("Mode", createGame.getMode());
                                    bundle.putSerializable("Customize", createGame.getCustomize());
                                    Intent intent = new Intent(GameSceneActivity.this, GameSceneActivity.class);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                    finish();
                                }
                            }).setNeutralButton("返回", (dialog, which) -> finish());
                    builder.show();
                    break;
                case CUSTOMIZE:
                    /*
                    * 自定义模式，到达终点需要做的事情与自定义的地图有关
                    * */
                    switch (customize) {
                        case CUSTOMIZE:
                            /*
                            * 自定义大小的地图，需要做如下事情：
                            * */
                            Log.d(getClass().getName(), "自定义大小地图: 玩家到达终点啦~用时：" + newRecord + " 秒！");
                            break;
                        case BIRTHDAY:
                            /*
                            * 生日地图，需要做如下事情：
                            * 0. 首次通过则完成成就
                            * 1. 更新存档数据
                            * 2. 弹出对话框显示完成
                            * */
                            if (!FileUtil.checkAchievementFinished(parent, achievement_fileName, "Happy Birthday!")) {
                                FileUtil.finishAchievementByName(parent, achievement_fileName, "Happy Birthday!");
                            };
                            int recordBirthday = Integer.parseInt(FileUtil.getValue(parent, data_fileName, "recordBirthday"));
                            FileUtil.setValue(parent, data_fileName, "recordBirthday", String.valueOf(recordBirthday + 1));
                            AlertDialog.Builder builderBirthday = new AlertDialog.Builder(GameSceneActivity.this);
                            builderBirthday.setTitle("Happy Birthday!").setMessage("生日快乐哦~偶尔放松放松，每天都要保持开心哈~😊");
                            builderBirthday.setNeutralButton("返回", (dialog, which) -> dialog.dismiss())
                                    .setOnDismissListener(dialog -> finish());
                            builderBirthday.show();
                            break;
                    }
                    break;
            }
        }
    }

    /**
     * 通关后使用提示框提示玩家
     * @param newRecord 通关的时间记录
     * @param isNew 是否为新纪录
     */
    private void notifyPlayer(int newRecord, boolean isNew) {
        AlertDialog.Builder builder = new AlertDialog.Builder(GameSceneActivity.this);
        if (isNew) {
            builder.setTitle("新纪录！").setIcon(R.drawable.destination);
        } else {
            builder.setTitle("恭喜通关");
        }
        builder.setMessage("到达终点啦~用时：" + newRecord + " 秒！");
        builder.setPositiveButton("确认", (dialog, which) -> finish())
                .setOnDismissListener(dialog -> finish());
        builder.show();
    }

    /**
     * 界面焦点改变时触发(防止切入后台再切回来时，布局参数被清除导致状态栏出现)
     * @param hasFocus true: 当前 activity 被选中
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            SystemUiUtil.hideSystemBar(getWindow());
        }
    }

    /**
     * 结束运行时停止计时器
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止计时器
        if (gameTimer != null) {
            gameTimer.stop();
        }
    }
}